package tree;

import h3d.prim.Disc;
import haxe.ds.GenericStack;
import haxe.ds.HashMap;

//枝条节点
class Tress extends Leaf{
    private var m_leafMap : Map<LeafKind, Leaf>;
    private var m_stack : GenericStack<LeafKind>;
    
    public function new() {
        super();
        m_leafMap = [];
        m_stack = new GenericStack<LeafKind>();
    }

    public override function OnUpdate(cmd:LeafCmd) {
        super.OnUpdate(cmd);
        var first = m_stack.first();
        if(first != null){
            m_leafMap[first].OnUpdate(cmd);
        }
    }

    //增加叶结点
    public function AddLeaf(kind:LeafKind, leaf:Leaf){
        leaf.SetParent(this);
        m_leafMap[kind] = leaf;
    }
    
    //移除叶结点
    public function RemoveLeaf(kind:LeafKind) {
        m_leafMap.remove(kind);
    }

    //将一个叶结点放入运行栈
    public function PushLeaf(kind:LeafKind) {
        var last = m_stack.first();
        m_stack.add(kind);
        m_leafMap[kind].OnEnter(last);
    }

    //弹出运行栈的第一个节点
    public function PopLeaf() {
        var kind = m_stack.pop();
        var next = m_stack.first();
        m_leafMap[kind].OnExit(next);
    }

}
